<?php

//require_once("member.class.php");
//require_once("basket.class.php");
//require_once("product.class.php");

class Producer extends AppModel
{
	//data members which correspond to database values for attributes of a producer

	var $name = 'Producer';
	var $primaryKey = 'producer_id';
	var $belongsTo = array(
		'Member' => array(
			'className'=>'Member',
			'fields'=> array(
				'username',
				'member_id',
				'auth_type',
				'business_name',
				'last_name',
				'first_name',
				'work_phone',
				'work_address_line1',
				'work_address_line2',
				'work_city',
				'work_state',
				'work_zip',
				'fax',
				'toll_free',
				'home_page',
				'email_address',
				'paid_fee',
				'memb_amt_owed'
			)
		)
	);
	var $hasMany = array(
		'Product' => array(
			'order' => 'Product.product_name'
		)
	);
	
	/***
	* Recommends an untaken producer ID based on the business name input
	*/
	function recommendProducerID($businessName) {
		$rec = preg_replace('/[a-z\s\W]/','',$businessName);
		$n = 1;
		do {
			$l = 5 - strlen($n);
			if(strlen($rec)>$l) {
				$rec = substr($rec,0,$l) . $n;
			} else {
				$rec = str_pad($rec,$l,'0') . $n;
			}
			$n++;
		} while ($this->idExists($rec));
		return $rec;
	}
	
	function idExists($id) {
		$found = $this->find($this->primaryKey . ' = "' . $id . '"');
		if (!empty($found)) {
			return true;
		} else {
			return false;
		}
	}
	
	function findProducersWithInvoices($del = null) {
		$d = ($del === null) ? Configure::read('Delivery.delivery_id') : $del;
		$sql = 'SELECT DISTINCT Producer.producer_id, Member.business_name, Member.address_line1, Member.address_line2, Member.city, Member.state, Member.zip, Member.work_address_line1, Member.work_address_line2, Member.work_city, Member.work_state, Member.work_zip FROM producers as Producer, members as Member, basket_items as Basket, product_list as Product, orderables as Orderable
		WHERE Member.member_id = Producer.member_id
		AND Product.producer_id = Producer.producer_id
		AND Orderable.product_id = Product.product_id
		AND Orderable.id = Basket.orderable_id
		AND Orderable.delivery_id = ' . $d
		. ' ORDER BY Member.business_name';
		
		$r = $this->query($sql);
		return $r;
	}
	
	function formatAddresses($producers) {
		Configure::write('debug', 0);
		$r = array();
		foreach($producers as $producer) {
			if (!empty($producer['Member']['business_name'])) {
				$a = array();
				$a[] = '<b>' . $producer['Member']['business_name'] . '</b>';
				$w = (!empty($producer['Member']['work_address_line1'])) ? 'work_' : '';
				$a[] = $producer['Member'][$w . 'address_line1'];
				if (!empty($producer['Member'][$w . 'address_line2'])) {
					$a[] = $producer['Member'][$w . 'address_line2'];
				}
				$c = (empty($producer['Member'][$w . 'city'])) ? '' : $producer['Member'][$w . 'city'] . ', ';
				$a[] = $c . $producer['Member'][$w . 'state'] . ' ' . $producer['Member'][$w . 'zip'];
				$r[] = $a;
			}
		}
		return $r;
	}
	
	function getMemberInfo($id) {
		$p = $this->find('Producer.producer_id = "' . $id . '"');
		return $this->Member->find('Member.member_id = ' . $p['Producer']['member_id']);
	}
	
	function businessName($id) {
		$m = $this->getMemberInfo($id);
		return $m['Member']['business_name'];
	}
	
	function unlistAll($jobInfo) {
		extract($jobInfo);
		$this->unbindModel(array('hasMany' => array('Product')));
		$producers = $this->findAll('donotlist_producer = "0"');
		foreach($producers as $prod) {
			$this->id = $prod['Producer']['producer_id'];
			$this->saveField('donotlist_producer', true);
		}
		$this->unbindModel(array('hasMany' => array('Product')));
		return $this->findAll();
	}
	
}

?>
